package com.thinkit.bigdata.web.controller.statistics;

import com.google.common.base.Strings;
import com.thinkit.bigdata.core.util.DateFormatUtil;
import com.thinkit.bigdata.core.util.es.ESUtil;
import com.thinkit.bigdata.core.util.excel.XlsUtil;
import com.thinkit.bigdata.web.model.es.audioEs.VoiceInfoDto;
import com.thinkit.bigdata.web.model.result.HtmlResult;
import com.thinkit.bigdata.web.service.config.AreaService;
import com.thinkit.bigdata.web.service.impl.audioSearch.AudioSearchPubMetImpl;
import com.thinkit.bigdata.web.service.sec.GroupService;
import com.thinkit.bigdata.web.service.statistics.PhoneStatisticsServer;
import com.thinkit.bigdata.web.service.statistics.RuleStaticsServer;
import com.thinkit.bigdata.web.service.statistics.TagStatisticsServer;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.*;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 话务统计
 * Created by hccl on 2018/10/29.
 */
@RestController
@RequestMapping("/phone-statistics")
public class StatisticsCont {

    private static Logger logger = Logger.getLogger(StatisticsCont.class);

    @Resource
    private PhoneStatisticsServer phoneServer;

    @Resource
    private TagStatisticsServer tagServer;

    @Resource
    private RuleStaticsServer ruleServer;

    @Resource
    private AreaService areaService;

    @Resource
    private GroupService groupService;

    @Resource
    private ESUtil esUtil;

    @RequestMapping(value = "/get-phone-count", method = RequestMethod.POST)
    public HtmlResult getPhoneCount(HttpServletRequest request) {
        String logTxt = "【话务量-统计】";
        logger.info(logTxt);
        String provinceCode = request.getParameter("provinceCode");
        String groupId = request.getParameter("groupId");
        String callStartTime = request.getParameter("callStartTime");
        if (Strings.isNullOrEmpty(callStartTime)) {
            return HtmlResult.failure("通话时间为空");
        }
        String callEndTime = request.getParameter("callEndTime");
        String seatNum = request.getParameter("seatNum");
        String customerNum = request.getParameter("customerNum");
        String reCallFlag = request.getParameter("reCallFlag");
        String satisfaction = request.getParameter("satisfaction");
        String pageNum = request.getParameter("pageNum");//起始页
        String pageSize = request.getParameter("pageSize");//每页条数
        logger.info(logTxt + "provinceCode = " + provinceCode);
        logger.info(logTxt + "groupId = " + groupId);
        logger.info(logTxt + "callStartTime = " + callStartTime);
        logger.info(logTxt + "callEndTime = " + callEndTime);
        logger.info(logTxt + "seatNum = " + seatNum);
        logger.info(logTxt + "customerNum = " + customerNum);
        logger.info(logTxt + "reCallFlag = " + reCallFlag);
        logger.info(logTxt + "satisfaction = " + satisfaction);
        logger.info(logTxt + "pageNum = " + pageNum);
        logger.info(logTxt + "pageSize = " + pageSize);
        try {
            return phoneServer.getPhoneCount(request);
        } catch (Exception e) {
            e.printStackTrace();
            return HtmlResult.failure("统计异常");
        }
    }

    @RequestMapping(value = "/export-phonemes", method = RequestMethod.POST)
    public void exportPhoneData(HttpServletRequest request, HttpServletResponse response) {
        String logTxt = "【话务量-统计-导出】";
        logger.info(logTxt);
        try {
            Map<String, List<Map<String, String>>> result = phoneServer.getPhoneQuery(request, "export");
            List<Map<String, String>> resultList = result.get("resultList");
            logger.info("获取数据条数为：" + resultList.size());
            //生成excel
            HSSFWorkbook workbook = new HSSFWorkbook();//创建一个xls文档对象
            HSSFSheet sheet = workbook.createSheet();//创建一个工作薄对象
            //设置标题和列名称
            String[] header = {"编号", "省份", "坐席班组", "坐席工号", "总通话时长(秒)",
                    "平均通话时长(秒)", "话务量", "呼出量", "接入量", "重复来电量"};
            String title = "话务量统计信息表";
            XlsUtil.setTableTitleHeader(workbook, sheet, Arrays.asList(header), title);
            //获取表文样式
            HSSFCellStyle tableStyle = XlsUtil.getTableStyle(workbook);
            HSSFRow row2;
            HSSFCell cell2;
            for (int j = 0; j < resultList.size(); j++) {
                Map<String, String> source = resultList.get(j);
                row2 = sheet.createRow(3 + j);
                for (int i = 0; i < header.length; i++) {
                    cell2 = row2.createCell(i);
                    cell2.setCellStyle(tableStyle);
                    switch (i) {
                        case 0:
                            cell2.setCellValue(new HSSFRichTextString((j + 1) + ""));
                            break;
                        case 1:
                            cell2.setCellValue(new HSSFRichTextString(areaService.selectByPrimaryCode(source.get("provinceCode") + "").getArea()));
                            break;
                        case 2:
                            if (source.get("groupId") == null || source.get("groupId").equals("") || "-1".equals(source.get("groupId") + "")) {
                                cell2.setCellValue(new HSSFRichTextString("-"));
                            } else {
                                cell2.setCellValue(new HSSFRichTextString(groupService.selectByPrimaryKey(Long.parseLong(source.get("groupId"))).getName()));
                            }
                            break;
                        case 3:
                            if (source.get("seatNum") == null || source.get("seatNum").equals("")) {
                                cell2.setCellValue(new HSSFRichTextString("-"));
                            } else {
                                cell2.setCellValue(new HSSFRichTextString(source.get("seatNum") + ""));
                            }
                            break;
                        case 4:
                            cell2.setCellValue(new HSSFRichTextString(source.get("holdDur") + ""));
                            break;
                        case 5:
                            cell2.setCellValue(new HSSFRichTextString(source.get("averageDur") + ""));
                            break;
                        case 6:
                            cell2.setCellValue(new HSSFRichTextString(source.get("phoneCount") + ""));
                            break;
                        case 7:
                            cell2.setCellValue(new HSSFRichTextString(source.get("callOutCount") + ""));
                            break;
                        case 8:
                            cell2.setCellValue(new HSSFRichTextString(source.get("callInCount") + ""));
                            break;
                        case 9:
                            cell2.setCellValue(new HSSFRichTextString(source.get("reCallFlag") + ""));
                            break;
                    }
                }
            }
            for (int i = 0; i < header.length; i++) {
                sheet.autoSizeColumn((short) i, true);//自动调整列宽
            }
            String fileName = title + "_" + DateFormatUtil.getNowTime() + ".xls";
            // 清空response
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));  //转码之后下载的文件名不会出现中文乱码
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(logTxt + "异常");
        }
    }

    @RequestMapping(value = "/export-sat-mes", method = RequestMethod.POST)
    public void exportSatData(HttpServletRequest request, HttpServletResponse response) {
        String logTxt = "【满意度-统计-导出】";
        logger.info(logTxt);
        try {
            Map<String, List<Map<String, String>>> result = phoneServer.getPhoneQuery(request, "export");
            List<Map<String, String>> resultList = result.get("resultList");
            logger.info("获取数据条数为：" + resultList.size());
            //生成excel
            HSSFWorkbook workbook = new HSSFWorkbook();//创建一个xls文档对象
            HSSFSheet sheet = workbook.createSheet();//创建一个工作薄对象
            //设置标题和列名称
            String[] header = {"编号", "省份", "坐席班组", "坐席工号", "通话时长(秒)",
                     "话务量", "呼出量", "接入量", "非常满意", "满意", "不满意", "无评价"};
            String title = "满意度统计信息表";
            XlsUtil.setTableTitleHeader(workbook, sheet, Arrays.asList(header), title);
            //获取表文样式
            HSSFCellStyle tableStyle = XlsUtil.getTableStyle(workbook);
            HSSFRow row2;
            HSSFCell cell2;
            for (int j = 0; j < resultList.size(); j++) {
                Map<String, String> source = resultList.get(j);
                row2 = sheet.createRow(3 + j);
                for (int i = 0; i < header.length; i++) {
                    cell2 = row2.createCell(i);
                    cell2.setCellStyle(tableStyle);
                    switch (i) {
                        case 0:
                            cell2.setCellValue(new HSSFRichTextString((j + 1) + ""));
                            break;
                        case 1:
                            cell2.setCellValue(new HSSFRichTextString(areaService.selectByPrimaryCode(source.get("provinceCode")).getArea()));
                            break;
                        case 2:
                            if (source.get("groupId") == null || source.get("groupId").equals("") || "-1".equals(source.get("groupId") + "")) {
                                cell2.setCellValue(new HSSFRichTextString("-"));
                            } else {
                                cell2.setCellValue(new HSSFRichTextString(groupService.selectByPrimaryKey(Long.parseLong(source.get("groupId"))).getName()));
                            }
                            break;
                        case 3:
                            if (source.get("seatNum") == null || source.get("seatNum").equals("")) {
                                cell2.setCellValue(new HSSFRichTextString("-"));
                            } else {
                                cell2.setCellValue(new HSSFRichTextString(source.get("seatNum") + ""));
                            }
                            break;
                        case 4:
                            cell2.setCellValue(new HSSFRichTextString(source.get("holdDur") + ""));
                            break;
                        case 5:
                            cell2.setCellValue(new HSSFRichTextString(source.get("phoneCount") + ""));
                            break;
                        case 6:
                            cell2.setCellValue(new HSSFRichTextString(source.get("callOutCount") + ""));
                            break;
                        case 7:
                            cell2.setCellValue(new HSSFRichTextString(source.get("callInCount") + ""));
                            break;
                        case 8:
                            cell2.setCellValue(new HSSFRichTextString(source.get("satVeryYes") + ""));
                            break;
                        case 9:
                            cell2.setCellValue(new HSSFRichTextString(source.get("satYes") + ""));
                            break;
                        case 10:
                            cell2.setCellValue(new HSSFRichTextString(source.get("satNo") + ""));
                            break;
                        case 11:
                            cell2.setCellValue(new HSSFRichTextString(source.get("satNull") + ""));
                            break;
                    }
                }
            }
            for (int i = 0; i < header.length; i++) {
                sheet.autoSizeColumn((short) i, true);//自动调整列宽
            }
            String fileName = title + "_" + DateFormatUtil.getNowTime() + ".xls";
            // 清空response
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));  //转码之后下载的文件名不会出现中文乱码
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            logger.info(logTxt + "异常");
        }
    }

    @RequestMapping(value = "/export-detail-data", method = RequestMethod.POST)
    public void exportDetailData(HttpServletRequest request, HttpServletResponse response) {
        String logTxt = "【明细-导出】";
        logger.info(logTxt);
        try {
            List<VoiceInfoDto> resultList = phoneServer.getPhoneDetailData(request);
            logger.info("获取数据条数为：" + resultList.size());
            if (resultList.isEmpty()) {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html;charset=utf-8");
                PrintWriter out = response.getWriter();
                out.print("没有话单信息");
                out.close();
            }
            //生成excel
            HSSFWorkbook workbook = new HSSFWorkbook();//创建一个xls文档对象
            HSSFSheet sheet = workbook.createSheet();//创建一个工作薄对象
            //设置标题和列名称
            String[] header = {"编号", "省份", "坐席班组", "坐席工号", "坐席姓名", "呼叫类型", "录音流水号", "客户号码",
                    "通话时长(秒)", "通话开始时间", "通话结束时间", "挂机方", "是否重复来电", "满意度"};
            String title = "明细";
            XlsUtil.setTableTitleHeader(workbook, sheet, Arrays.asList(header), title);
            //获取表文样式
            HSSFCellStyle tableStyle = XlsUtil.getTableStyle(workbook);
            HSSFRow row2;
            HSSFCell cell2;
            for (int j = 0; j < resultList.size(); j++) {
                VoiceInfoDto source = resultList.get(j);
                row2 = sheet.createRow(3 + j);
                for (int i = 0; i < header.length; i++) {
                    cell2 = row2.createCell(i);
                    cell2.setCellStyle(tableStyle);
                    switch (i) {
                        case 0:
                            cell2.setCellValue(new HSSFRichTextString((j + 1) + ""));
                            break;
                        case 1:
                            cell2.setCellValue(new HSSFRichTextString(source.getProvince()));
                            break;
                        case 2:
                            cell2.setCellValue(new HSSFRichTextString(source.getSeatGroup()));
                            break;
                        case 3:
                            cell2.setCellValue(new HSSFRichTextString(source.getSeatId()));
                            break;
                        case 4:
                            cell2.setCellValue(new HSSFRichTextString(source.getSeatName()));
                            break;
                        case 5:
                            cell2.setCellValue(new HSSFRichTextString(source.getCallDirection() == 0 ? "呼入" : "呼出"));
                            break;
                        case 6:
                            cell2.setCellValue(new HSSFRichTextString(source.getSerialNumber()));
                            break;
                        case 7:
                            cell2.setCellValue(new HSSFRichTextString(source.getCustomerNum()));
                            break;
                        case 8:
                            cell2.setCellValue(new HSSFRichTextString(source.getHoldDuration() + ""));
                            break;
                        case 9:
                            cell2.setCellValue(new HSSFRichTextString(source.getCallStartTime()));
                            break;
                        case 10:
                            cell2.setCellValue(new HSSFRichTextString(source.getCallEndTime()));
                            break;
                        case 11:
                            cell2.setCellValue(new HSSFRichTextString(esUtil.changeOnHook(source.getOnHook())));
                            break;
                        case 12:
                            cell2.setCellValue(new HSSFRichTextString(source.getReCallFlag() == 0 ? "否" : "是"));
                            break;
                        case 13:
                            cell2.setCellValue(new HSSFRichTextString(esUtil.changeSatisfaction(source.getSatisfaction())));
                            break;
                    }
                }
            }
            for (int i = 0; i < header.length; i++) {
                sheet.autoSizeColumn((short) i, true);//自动调整列宽
            }
            String fileName = title + "_" + DateFormatUtil.getNowTime() + ".xls";
            // 清空response
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));  //转码之后下载的文件名不会出现中文乱码
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            logger.info(logTxt + "异常");
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/get-tag-by-mes", method = RequestMethod.POST)
    public HtmlResult searchTagMes(HttpServletRequest request) {
        String logTxt = "【标签-统计-音频列表查询】";
        logger.info(logTxt);
        String pageNum = request.getParameter("pageNum");//起始页
        String pageSize = request.getParameter("pageSize");//每页条数
        String sortName = request.getParameter("sortName");//录音流水
        String sortOrder = request.getParameter("sortOrder");//录音流水
        logger.info(logTxt + "pageNum = " + pageNum);
        logger.info(logTxt + "pageSize = " + pageSize);
        logger.info(logTxt + "sortName = " + sortName);
        logger.info(logTxt + "sortOrder = " + sortOrder);
        try {
            return tagServer.searchData(request);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(logTxt + "获取数据异常");
            return HtmlResult.failure("获取数据异常");
        }
    }

    @RequestMapping(value = "/get-tag-count", method = RequestMethod.POST)
    public HtmlResult getTagCount(HttpServletRequest request) {
        String logTxt = "【标签-统计】";
        logger.info(logTxt);
        String provinceCode = request.getParameter("provinceCode");
        String citys = request.getParameter("citys");
        String callStartTime = request.getParameter("callStartTime");
        if (Strings.isNullOrEmpty(callStartTime)) {
            return HtmlResult.failure("通话时间为空");
        }
        String callEndTime = request.getParameter("callEndTime");
        String sysTagIds = request.getParameter("sysTagIds");//系统标签_标签ids
        String telTagIds = request.getParameter("telTagIds");//话务小结_标签ids
        String pageNum = request.getParameter("pageNum");//起始页
        String pageSize = request.getParameter("pageSize");//每页条数
        logger.info(logTxt + "provinceCode = " + provinceCode);
        logger.info(logTxt + "citys = " + citys);
        logger.info(logTxt + "callStartTime = " + callStartTime);
        logger.info(logTxt + "callEndTime = " + callEndTime);
        logger.info(logTxt + "sysTagIds = " + sysTagIds);
        logger.info(logTxt + "telTagIds = " + telTagIds);
        logger.info(logTxt + "pageNum = " + pageNum);
        logger.info(logTxt + "pageSize = " + pageSize);
        return tagServer.getTagCount(request);
    }

    @RequestMapping(value = "/export-tag-mes", method = RequestMethod.POST)
    public void exportTagData(HttpServletRequest request, HttpServletResponse response) {
        String logTxt = "【标签-统计-导出】";
        logger.info(logTxt);
        try {
            Map<String, List<Map<String, String>>> result = tagServer.getTagQuery(request, "export");
            List<Map<String, String>> resultList = result.get("resultList");
            logger.info("获取数据条数为：" + resultList.size());
            //生成excel
            HSSFWorkbook workbook = new HSSFWorkbook();//创建一个xls文档对象
            HSSFSheet sheet = workbook.createSheet();//创建一个工作薄对象
            //设置标题和列名称
            String[] header = {"编号", "省份", "标签属性", "标签/话务小结", "总打标量", "系统打标量", "人工打标量", "人工纠正量", "系统正确率", "系统漏识率"};
            String title = "标签统计信息表";
            XlsUtil.setTableTitleHeader(workbook, sheet, Arrays.asList(header), title);
            //获取表文样式
            HSSFCellStyle tableStyle = XlsUtil.getTableStyle(workbook);
            HSSFRow row2;
            HSSFCell cell2;
            for (int j = 0; j < resultList.size(); j++) {
                Map<String, String> source = resultList.get(j);
                row2 = sheet.createRow(3 + j);
                for (int i = 0; i < header.length; i++) {
                    cell2 = row2.createCell(i);
                    cell2.setCellStyle(tableStyle);
                    switch (i) {
                        case 0:
                            cell2.setCellValue(new HSSFRichTextString((j + 1) + ""));
                            break;
                        case 1:
                            cell2.setCellValue(new HSSFRichTextString(areaService.selectByPrimaryCode(source.get("provinceCode")).getArea()));
                            break;
                        case 2:
                            if (source.get("tagType") == null || "".equals(source.get("tagType"))) {
                                cell2.setCellValue(new HSSFRichTextString("-"));
                            } else {
                                cell2.setCellValue(new HSSFRichTextString(source.get("tagType").equals("0") ? "话务小结" : "系统标签"));
                            }
                            break;
                        case 3:
                            cell2.setCellValue(new HSSFRichTextString(source.get("tagName") + ""));
                            break;
                        case 4:
                            cell2.setCellValue(new HSSFRichTextString(source.get("tagCount") + ""));
                            break;
                        case 5:
                            cell2.setCellValue(new HSSFRichTextString(source.get("sysTagCount") + ""));
                            break;
                        case 6:
                            cell2.setCellValue(new HSSFRichTextString(source.get("addTagCount") + ""));
                            break;
                        case 7:
                            cell2.setCellValue(new HSSFRichTextString(source.get("corTagCount") + ""));
                            break;
                        case 8:
                            cell2.setCellValue(new HSSFRichTextString(source.get("accuracy") + ""));
                            break;
                        case 9:
                            cell2.setCellValue(new HSSFRichTextString(source.get("leakage") + ""));
                            break;
                    }
                }
            }
            for (int i = 0; i < header.length; i++) {
                sheet.autoSizeColumn((short) i, true);//自动调整列宽
            }
            String fileName = title + "_" + DateFormatUtil.getNowTime() + ".xls";
            // 清空response
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));  //转码之后下载的文件名不会出现中文乱码
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            logger.info(logTxt + "异常");
        }
    }

    @RequestMapping(value = "/export-data-mes", method = RequestMethod.POST)
    public void exportMyAss(HttpServletRequest request, HttpServletResponse response) {
        String logTxt = "【话单信息-导出】";
        logger.info(logTxt);
        try {
            BoolQueryBuilder boolQuery = ruleServer.getQuery(logTxt, request);
            new AudioSearchPubMetImpl().exportMyAss(logTxt, request, response, boolQuery, esUtil);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/get-rule-count", method = RequestMethod.POST)
    public HtmlResult getRuleCount(HttpServletRequest request) {
        String logTxt = "【规则-统计】";
        logger.info(logTxt);
        String workId = request.getParameter("workId");
        if (Strings.isNullOrEmpty(workId)) {
            return HtmlResult.failure("任务为空");
        }
        try {
            return ruleServer.getRuleCount(request, logTxt);
        } catch (Exception e) {
            e.printStackTrace();
            return HtmlResult.failure("统计异常");
        }
    }

    @RequestMapping(value = "/export-rule-mes", method = RequestMethod.POST)
    public void exportRuleData(HttpServletRequest request, HttpServletResponse response) {
        String logTxt = "【规则-统计-导出】";
        logger.info(logTxt);
        try {
            Map<String, List<Map<String, String>>> result = ruleServer.getRuleQuery(request, "export", logTxt);
            List<Map<String, String>> resultList = result.get("resultList");
            logger.info("获取数据条数为：" + resultList.size());
            //生成excel
            HSSFWorkbook workbook = new HSSFWorkbook();//创建一个xls文档对象
            HSSFSheet sheet = workbook.createSheet();//创建一个工作薄对象
            //设置标题和列名称
            String[] header = {"编号", "省份", "坐席班组", "坐席工号", "规则名称", "规则分类", "规则属性", "分值属性", "致命性", "总命中量", "系统命中量",
                    "人工添加量", "人工纠正量", "申诉量", "驳回量", "申诉成功量", "申诉率", "申诉成功率", "系统正确率", "系统漏识率"};
            String title = "规则统计信息表";
            XlsUtil.setTableTitleHeader(workbook, sheet, Arrays.asList(header), title);
            //获取表文样式
            HSSFCellStyle tableStyle = XlsUtil.getTableStyle(workbook);
            HSSFRow row2;
            HSSFCell cell2;
            for (int j = 0; j < resultList.size(); j++) {
                Map<String, String> source = resultList.get(j);
                row2 = sheet.createRow(3 + j);
                for (int i = 0; i < header.length; i++) {
                    cell2 = row2.createCell(i);
                    cell2.setCellStyle(tableStyle);
                    switch (i) {
                        case 0:
                            cell2.setCellValue(new HSSFRichTextString((j + 1) + ""));
                            break;
                        case 1:
                            if (source.get("provinceCode") == null || "".equals(source.get("provinceCode"))) {
                                cell2.setCellValue(new HSSFRichTextString("-"));
                            } else {
                                cell2.setCellValue(new HSSFRichTextString(areaService.selectByPrimaryCode(source.get("provinceCode")).getArea()));
                            }
                            break;
                        case 2:
                            if (source.get("groupId") == null || "".equals(source.get("groupId"))) {
                                cell2.setCellValue(new HSSFRichTextString("-"));
                            } else {
                                cell2.setCellValue(new HSSFRichTextString(groupService.groupGetByPK(Long.parseLong(source.get("groupId"))).getName()));
                            }
                            break;
                        case 3:
                            if (source.get("seatId") == null || "".equals(source.get("seatId"))) {
                                cell2.setCellValue(new HSSFRichTextString("-"));
                            } else {
                                cell2.setCellValue(new HSSFRichTextString(source.get("seatId") + ""));
                            }
                            break;
                        case 4:
                            cell2.setCellValue(new HSSFRichTextString(source.get("ruleName") + ""));
                            break;
                        case 5:
                            cell2.setCellValue(new HSSFRichTextString(source.get("ruleType") + ""));
                            break;
                        case 6:
                            cell2.setCellValue(new HSSFRichTextString(source.get("useFor").equals("0") ? "系统" : "人工"));
                            break;
                        case 7:

                            cell2.setCellValue(new HSSFRichTextString(source.get("fateful").equals("-1") ? "加分规则" : "减分规则"));
                            break;
                        case 8:
                            String fateFul = "";
                            if (!"-1".equals(source.get("fateful"))) {
                                fateFul = source.get("fateful").equals("0") ? "否" : "是";
                            }
                            cell2.setCellValue(new HSSFRichTextString(fateFul));
                            break;
                        case 9:
                            cell2.setCellValue(new HSSFRichTextString(source.get("hitCount") + ""));
                            break;
                        case 10:
                            cell2.setCellValue(new HSSFRichTextString(source.get("sysHitCount") + ""));
                            break;
                        case 11:
                            cell2.setCellValue(new HSSFRichTextString(source.get("leakageCount") + ""));
                            break;
                        case 12:
                            cell2.setCellValue(new HSSFRichTextString(source.get("accuracyCount") + ""));
                            break;
                        case 13:
                            cell2.setCellValue(new HSSFRichTextString(source.get("appealCount") + ""));
                            break;
                        case 14:
                            cell2.setCellValue(new HSSFRichTextString(source.get("rejectCount") + ""));
                            break;
                        case 15:
                            cell2.setCellValue(new HSSFRichTextString(source.get("appealOkCount") + ""));
                            break;
                        case 16:
                            cell2.setCellValue(new HSSFRichTextString(source.get("appeal") + ""));
                            break;
                        case 17:
                            cell2.setCellValue(new HSSFRichTextString(source.get("appealOk") + ""));
                            break;
                        case 18:
                            cell2.setCellValue(new HSSFRichTextString(source.get("accuracy") + ""));
                            break;
                        case 19:
                            cell2.setCellValue(new HSSFRichTextString(source.get("leakage") + ""));
                            break;
                    }
                }
            }
            for (int i = 0; i < header.length; i++) {
                sheet.autoSizeColumn((short) i, true);//自动调整列宽
            }
            String fileName = title + "_" + DateFormatUtil.getNowTime() + ".xls";
            // 清空response
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));  //转码之后下载的文件名不会出现中文乱码
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(logTxt + "异常");
            try {
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html;charset=utf-8");
                PrintWriter out = response.getWriter();
                out.print(HtmlResult.failure("导出异常"));
                out.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}
